//
//  EmoticonKeyBoardView.m
//  WLEmotionKeyBoard
//
//  Created by wanglei on 16/12/6.
//  Copyright © 2016年 wanglei. All rights reserved.
//
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import "EmoticonKeyBoardView.h"
//http://www.jianshu.com/p/064b5b82a3a2
static const CGFloat toolBarHeight = 36;
@implementation EmoticonKeyBoardView

//重写构造方法
- (instancetype)initWithselectEmoticon{
    CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 224);
    self = [super initWithFrame:rect];
    self.backgroundColor = [UIColor redColor];
    EmoticonManager *emoticonManager = [[EmoticonManager alloc]init];
    self.emoticonManager = emoticonManager;
    [self setUpui];
    return self;
}

//设置界面
- (void)setUpui{
    UIToolbar *toolBar = [[UIToolbar alloc]init];
    self.toolbar = toolBar;
    self.keyCollectionView = [[collectionView alloc]init];
    [self addSubview:_toolbar];
    [self addSubview:_keyCollectionView];
    [_toolbar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.equalTo(toolBarHeight);
    }];
    [_keyCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.bottom.equalTo(_toolbar.mas_top);
    }];
    [self setToolBar];
}

//设置工具条
- (void)setToolBar{
   NSMutableArray <UIBarButtonItem *>*items= [[NSMutableArray alloc]init];
    NSInteger index = 0;
    NSLog(@"self.emoticonManager.packages = %@",self.emoticonManager.packages);
    for (EmoticonPackages *p in self.emoticonManager.packages) {
        UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        NSLog(@"group_name_cn = %@",p.group_name_cn);
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:p.group_name_cn style:UIBarButtonItemStylePlain target:self action:@selector(itemDidClick:)];
        item.tag = index ++;
        [items addObject:space];
        [items addObject:item];
        [items addObject:space];
    }
    self.toolbar.items = items;
}

//按钮的监听方法
- (void)itemDidClick:(UIBarButtonItem *)item{
    NSIndexPath *indexpath= [NSIndexPath indexPathForItem:0 inSection:item.tag];
    [self.keyCollectionView scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}


@end
